// mycomponent/feedback-list/feedback-list.js
const app = getApp()
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: Array,
      value: []
    },
    type: {
      type: String,
      value: 'feedback'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    complaintsShow: false,
    complaintsItem: {},
    complaints: '',
    loading: false,
    labelText: '评价'
  },

  lifetimes: {
    attached: function() {
      if (this.data.type == 'complaints') {
        this.setData({ labelText: '投诉' })
      }
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 跳转至详情
     * @param {Object} e 
     */
    goDetail: function(e) {
      const { type } = this.data
      const { item } = e.currentTarget.dataset
      const info = encodeURIComponent(JSON.stringify(item))
      wx.navigateTo({
        url: '/packageFT/guide/feedback-detail/feedback-detail?feedbackInfo=' + info + '&type=' + this.data.type,
      })
    },

    /**
     * 显示申诉弹窗
     * @param {Object} e 
     */
    showComplaints: function(e) {
      const { item } = e.currentTarget.dataset
      this.setData({
        complaintsShow: true,
        complaintsItem: item
      })
    },

    /**
     * 关闭申诉弹窗
     */
    complaintsClose: function() {
      this.setData({
        complaintsShow: false,
        complaintsItem: {},
        complaints: ''
      })
    },

    /**
     * 提交申诉
     */
    sumitComplaints: function() {
      const { complaints, complaintsItem } = this.data
      if (!complaints) {
        app.tips('请输入申诉内容')
        return
      }
      const urlStr = app.getNetAddresss('plugin.guide.frontend.complain.appeal')
      const data = {
        complain_id: complaintsItem.id,
        content: complaints
      }
      const _this = this
      this.setData({ loading: true })
      app._postNetWork({
        url: urlStr,
        data,
        success: function(res) {
          const resData = res.data
          if (resData.result == 1) {
            _this.complaintsClose()
            wx.showModal({
              title: '成功',
              content: '提交成功，平台人员将会为您处理该申诉！',
              showCancel: false,
              complete: (resC) => {
                if (resC.confirm) {
                  _this.triggerEvent('refreshList')
                }
              }
            })
            _this.setData({ complainShow: false })
          } else {
            app.tips(resData.msg || '提交失败！')
          }
        },
        complete: function() {
          _this.setData({ loading: false })
        }
      })
    }
  }
})